package ar.com.larreta.utilidades;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;

public class UtilidadesFlujoEntrada {
	
	protected static Logger logger = Logger.getLogger(UtilidadesFlujoEntrada.class);

	private InputStream stream;
	
	public UtilidadesFlujoEntrada(InputStream stream){
		this.stream = stream;
	}
	
	/**
	 * Retorna un archivo temporal que representa el flujo 
	 * @return
	 */
	public File getArchivo(){
		try {
			GeneradorClavesUnicas clave = new GeneradorClavesUnicas(); 
			File temporal = File.createTempFile("ufe", clave.getPk());
			FileOutputStream outputStream = FileUtils.openOutputStream(temporal);
			try {
                IOUtils.copy(stream, outputStream);
            } finally {
                IOUtils.closeQuietly(outputStream);
            }
			return temporal;
		} catch (IOException e) {
			logger.error(e);
		}
		return null;
	}
	
}
